package com.gandong8.trace.tool.config.mysql;

import com.baomidou.mybatisplus.annotation.DbType;
//import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.gandong8.trace.tool.core.sql.PerformanceInterceptor;
import com.gandong8.trace.tool.core.sql.SqlInterceptor;
import com.gandong8.trace.tool.core.sql.SqlLogImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * https://baomidou.com/guide/page.html
 * https://mybatis.plus/guide/interceptor.html#mybatisplusinterceptor
 */
@Configuration
public class MybatisPlusConf {


//	@Bean
//	public PaginationInterceptor paginationInterceptor(){
//		return new PaginationInterceptor();
//	}


    @Bean("mybatisPlusPageInterceptor")
    public MybatisPlusInterceptor mybatisPlusPageInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        //mybatis plus分页查询返回结果默认上限是500条， -1则不受限制
        paginationInnerInterceptor.setMaxLimit(1000L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }



//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
//        return configuration -> configuration.setUseDeprecatedExecutor(false);
//    }

    @Bean("mybatisConfiguration")
    public MybatisConfiguration mybatisConfiguration(@Qualifier("mybatisPlusPageInterceptor") MybatisPlusInterceptor mybatisPlusPageInterceptor) {
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(SqlLogImpl.class);
        configuration.addInterceptor(mybatisPlusPageInterceptor);
        configuration.addInterceptor(new SqlInterceptor());
        return configuration;
    }

    //动态分表
    //@Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        HashMap<String, TableNameHandler> map = new HashMap<>();
        map.put("test", new MybatisTableParser());
        dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;
    }

    //sql效率分析,mybatis-plus3.2.0以上版本已经移除官方推荐使用第三方扩展(p6spy)
    //@Bean("performanceInterceptor")
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        //sql最大执行时长
        performanceInterceptor.setMaxTime(5000);
        //是否格式化sql默认false
        performanceInterceptor.setFormat(true);
        return new PerformanceInterceptor();
    }

}
